<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="关键字" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.customerType" clearable placeholder="客户类型">
          <el-option v-for="item in usertype" :key="item.id" :label="item.label" :value="item.id">
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.status" clearable placeholder="客户状态">
          <el-option v-for="item in userstatus" :key="item.id" :label="item.label" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button
	        v-if="isAuth('hotel:messageboard:save')"
	        type="primary"
	        @click="addOrUpdateHandle()"
	      >新增</el-button>-->

        <el-button v-if="isAuth('hotel:agreement:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>

        <el-button v-if="isAuth('hotel:agreement:save')" type="danger" @click="showsave('')">新增</el-button>
      </el-form-item>
      <el-form-item label="" v-if="isAuth('hotel:hotelseller:list')">
        <el-select multiple collapse-tags clearable style="width:180px;" v-model="sellerIds" filterable remote
          reserve-keyword @change="getDataList()" placeholder="商家名称" :remote-method="remoteMethod" :loading="loadingsa">
          <el-option v-for="item in platformSeller" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      
      
    </el-form>
    <el-table :data="dataListArray" border="" v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
       <el-table-column prop="sellerName" header-align="center" align="center" label="商家"></el-table-column>
      <el-table-column prop="accountNo" header-align="center" align="center" label="账号"></el-table-column>
      <el-table-column prop="customerNo" header-align="center" align="center" label="客户编号"></el-table-column>
      <el-table-column prop="customerName" header-align="center" align="center" label="客户名称"></el-table-column>
      <el-table-column prop="statusName" header-align="center" align="center" label="客户状态"></el-table-column>
      <el-table-column prop="customerTypeName" header-align="center" align="center" label="客户类型"></el-table-column>
      <el-table-column prop="customerIndustryName" header-align="center" align="center" label="客户行业"></el-table-column>
      <el-table-column prop="contacts" header-align="center" align="center" label="联系人"></el-table-column>
      <el-table-column prop="phone" header-align="center" align="center" label="联系电话"></el-table-column>
      <el-table-column prop="companyPhone" header-align="center" align="center" label="公司电话"></el-table-column>
      <el-table-column prop="fax" header-align="center" align="center" label="传真"></el-table-column>
      <el-table-column prop="region" header-align="center" align="center" label="地区"></el-table-column>
      <el-table-column prop="email" header-align="center" align="center" label="Email"></el-table-column>
      <el-table-column prop="accountStatus" header-align="center" align="center" label="账号状态">
        <template slot-scope="scope">
          <el-tag :type="coloracount[scope.row.accountStatus]" @click="updateStatus(scope.row.id,0,scope.row.accountStatus)">{{accountStatusMap[scope.row.accountStatus]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="examineStatus" header-align="center" align="center" label="审核状态">
        <template slot-scope="scope">
          <el-tag :type="coloracounts[scope.row.examineStatus]" @click="updateStatus(scope.row.id,1,scope.row.examineStatus)">{{examineStatusMap[scope.row.examineStatus]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" header-align="center" align="center" label="审核时间"></el-table-column>


      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showdetails(scope.row)">详情</el-button>
          <el-button type="text" size="small" @click="showsave(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"
	  ></el-pagination>

    <agreementadd ref="agreementadd" v-if="addOrUpdateVisible" :cancelReturn="cancelReturn"></agreementadd>

	  <el-dialog width="80%" :title="textMap[dialogStatus]" :visible.sync="showdetailsVisible" v-if="showdetailsVisible">
	      <agreementdetails :returnAgreemDetails="returnAgreemDetails" :mainid="id" :agreementNo="agreementNo" :customerJcName="customerJcName" :customerName="customerName"></agreementdetails>
	  </el-dialog>
	  </div>
</template>

<script>
  import agreementadd from "./agreement-add-or-update.vue";
  import agreementdetails from "./agreement-details.vue";
  export default {
    components: {
      agreementadd,
      agreementdetails
    },
    data() {
      return {
        id: '',
        agreementNo: '',
        customerJcName: '',
        customerName: '',
        dataForm: {
          key: "",
          customerType: "",
          status: "",
          limit: "",
          page: ""
        },
        sellerIds:[],
        usertype: [],
        coloracount: {
          0: "success",
          1: "danger"
        },
        coloracounts: {
          0: "danger",
          1: "success"
        },
        accountStatusMap: {
          0: '正常',
          1: '停用'
        },
        examineStatusMap: {
          0: '未审核',
          1: '审核'
        },
        textMap: {
          update: '编辑',
          create: '创建'
        },
        userstatus: [],
        platformSeller:[],
        dialogStatus: 'create',
        dataListArray: [],
        pageIndex: 1,
		loadingsa:false,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        showdetailsVisible: false
      };
    },
    activated() {
      this.getDataList();
      this.getagreementType('usertype');
      this.getagreementType('userstatus');
	  this.remoteMethod("");
    },
    methods: {
      
      //加载平台商家数据
      remoteMethod(query) {
        if (this.isAuth('hotel:hotelseller:list')) {
          this.loadingsa = true;
          this.$http({
            url: this.$http.adornUrl("/hotel/hotelseller/platformSeller"),
            method: "get",
            params: this.$http.adornParams({
              sellerName: query
            })
          }).then(({
            data
          }) => {
            this.loadingsa = false;
            if (data && data.code === 0) {
              this.platformSeller = data.page.list;
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      },
      returnAgreemDetails() {
        this.showdetailsVisible = false;
      },
      showdetails(row) {
        this.id = row.id;
        this.agreementNo = row.accountNo;
        this.customerName = row.customerName;
        this.customerJcName = row.customerJcname;
        this.showdetailsVisible = true;
      },
      updateStatus(id, type, val) {
        this.$http({
          url: this.$http.adornUrl("/hotel/agreement/updateStatus/" + id + "/" + type + "/" + val),
          method: "post"
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500
            });
          } else {
            this.$message.error(data.msg);
          }
          this.getDataList();
        });
      },
      cancelReturn() {
        this.addOrUpdateVisible = false;
        this.getDataList();
      },
      getagreementType(type) {
        this.$http({
          url: this.$http.adornUrl('/hotel/thotelagreementtype/list'),
          method: 'get',
          params: this.$http.adornParams({
            page: 1,
            limit: 999,
            type: type
          })
        }).then(({
          data
        }) => {
          if (type == 'usertype') {
            this.usertype = data.page.list;
          }
          if (type == 'userstatus') {
            this.userstatus = data.page.list;
          }
        });
      },
      showsave(id) {
        if (!id) {
          this.dialogStatus = "create";
        } else {
          this.dialogStatus = "update";
        }
        if (this.dialogStatus == "create") {
          this.id = "";
          this.addOrUpdateVisible = true;
          this.$nextTick(()=>{
            this.$refs.agreementadd.init(id);
          })
        } else {
          this.id = id;
          this.addOrUpdateVisible = true;
          this.$nextTick(()=>{
            this.$refs.agreementadd.init(id);
          })
        }
      },
      // 获取数据列表
      getDataList() {
        this.dataForm.limit = this.pageSize;
        this.dataForm.page = this.pageIndex;
        this.dataListLoading = true;
        var ids = ""
        if (this.sellerIds) {
          this.sellerIds.forEach((item, index) => {
            ids += item + ","
          })
          ids = ids.substring(0, ids.length - 1)
        }
          this.dataForm.ids = ids;
        this.$http({
          url: this.$http.adornUrl("/hotel/agreement/list"),
          method: "get",
          params: this.dataForm
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataListArray = [];
            this.dataListArray = data.page.list;
            this.totalPage = data.page.sum;
            this.pageIndex = data.page.page;
            this.$forceUpdate();
            console.log(this.dataListArray)

          } else {
            this.dataListArray = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val;
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id);
        });
      },
      // 删除
      deleteHandle(id) {
        var ids = id ?
          [id] :
          this.dataListSelections.map(item => {
            return item.id;
          });
        this.$confirm(
          `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
          "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        ).then(() => {
          this.$http({
            url: this.$http.adornUrl("/hotel/agreement/delete/" + ids),
            method: "post"

          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        });
      }
    }
  };
</script>

<style lang="scss" scoped>
.agreement-addorupdate-dialog /deep/ .el-dialog {
	width: 50%;
}
.agreement-addorupdate-dialog /deep/ .el-dialog__body {
	overflow: hidden;
}
@media only screen and (max-width: 1280px) {
.agreement-addorupdate-dialog /deep/ .el-dialog {
	width: 75%;
  }
}
</style>
